package nl.ajonker.nols.android.pl;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import nl.ajonker.nols.android.R;
import nl.ajonker.railroad.dl.SwitchRail;

import java.util.List;

public class RailAdapter extends ArrayAdapter<SwitchRail> {

    private final Context context;
    private final int layoutResourceId;
    private final List<SwitchRail> rails;

    public RailAdapter(Context context, int layoutResourceId, List<SwitchRail> rails) {
        super(context, layoutResourceId, rails);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.rails = rails;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RailHolder holder;


        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new RailHolder();
            holder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        } else {
            holder = (RailHolder) row.getTag();
        }

        SwitchRail rail = rails.get(position);
        holder.rail = rail;
        holder.txtTitle.setText("Wissel " + rail.getName());
        holder.imgIcon.setImageResource(R.drawable.left_green);

        return row;
    }

    public class RailHolder {
        ImageView imgIcon;
        TextView txtTitle;
        SwitchRail rail;

        public SwitchRail getRail() {
            return rail;
        }
    }
}